package com.lc.tgxm.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.tgxm.BaseApplication;
import com.lc.tgxm.R;
import com.lc.tgxm.fragment.FourFragment;
import com.lc.tgxm.fragment.TestFragment;
import com.lc.tgxm.fragment.ThreeFragment;
import com.lc.tgxm.fragment.TwoFragment;
import com.lc.tgxm.util.Log;
import com.zcx.helper.app.AppApplication;
import com.zcx.helper.fragment.navigation.NavigationManager;
import com.zcx.helper.fragment.navigation.NavigationManagerFactory;

/**
 * MainNavigationActivity
 * Created by Lei on 2017/01/10.
 */
public class MainNavigationActivity extends BaseActivity implements OnClickListener {
    private View home, tjfa, my, more;
    private NavigationManager<Fragment> navigationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenView(R.layout.activity_navigation, R.string.app_name);

        navigationManager = NavigationManagerFactory.createV4(this, R.id.navigation_frame);
        // noinspection unchecked
        navigationManager.addFragment(TestFragment.class, TwoFragment.class, ThreeFragment.class, FourFragment.class);

        (home = findViewById(R.id.nav_home)).setOnClickListener(this);
        (tjfa = findViewById(R.id.nav_tjfa)).setOnClickListener(this);
        (my = findViewById(R.id.nav_my)).setOnClickListener(this);
        (more = findViewById(R.id.nav_more)).setOnClickListener(this);

        onClick(home);

        // app初始化方法，主要通过动态权限,加载本地各种缓存文件夹
        applyPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @Override
    protected void onPermissionSuccess() {
        BaseApplication.INSTANCE.initAppRoot(this, new AppApplication.OnAppRootCallBack() {
            @Override
            public void onSuccess() {
                Log.i(TAG, "initAppRoot--->onSuccess");
            }
        });
    }

    @Override
    public void onClick(View view) {
        acInitView(home, R.mipmap.ic_launcher);
        acInitView(tjfa, R.mipmap.ic_launcher);
        acInitView(my, R.mipmap.ic_launcher);
        acInitView(more, R.mipmap.ic_launcher);

        switch (view.getId()) {
            case R.id.nav_home:
                clickTab(view, R.string.tab1, R.mipmap.ic_launcher, TestFragment.class);
                break;
            case R.id.nav_tjfa:
                clickTab(view, R.string.tab2, R.mipmap.ic_launcher, TwoFragment.class);
                break;
            case R.id.nav_my:
                clickTab(view, R.string.tab3, R.mipmap.ic_launcher, ThreeFragment.class);
                break;
            case R.id.nav_more:
                clickTab(view, R.string.tab4, R.mipmap.ic_launcher, FourFragment.class);
                break;
        }
    }

    private void clickTab(View view, int textResid, int imgResid, Class<? extends Fragment> cls) {
        setTitleName(textResid);
        acView(view, imgResid);
        navigationManager.show(cls);
    }

    // 默认文字颜色 不要在这里修改颜色，去colors.xml颜色配置文件修改taboff
    private void acInitView(View view, int mipmapId) {
        activateView(view, mipmapId, ContextCompat.getColor(this, R.color.taboff));
    }

    // 选中文字颜色 不要在这里修改颜色，去colors.xml颜色配置文件修改tabon
    private void acView(View view, int mipmapId) {
        activateView(view, mipmapId, ContextCompat.getColor(this, R.color.tabon));
    }

    private void activateView(View view, int mipmapId, int colorId) {
        try {
            ViewGroup viewGroup = (ViewGroup) view;
            ((ImageView) viewGroup.getChildAt(0)).setImageResource(mipmapId);
            ((TextView) viewGroup.getChildAt(1)).setTextColor(colorId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}